﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Eugenhome.Atrium.Bookmark
{
    /// <summary>
    /// 
    /// </summary>
    public class DynDNSController
    {
        /// <summary></summary>
        private string _dataPath;

        /// <summary></summary>
        private string _fileName;

        /// <summary></summary>
        public string User;
        /// <summary></summary>
        public string Host;
        /// <summary></summary>
        public string LastUpdate;
        /// <summary></summary>
        public bool Loaded = false;

        /// <summary></summary>
        private string _dataFullFileName { get { return Path.Combine(_dataPath, _fileName + ".xml"); } }
        /// <summary></summary>
        private string _logFullFileName { get { return Path.Combine(_dataPath, _fileName + ".log"); } }

        /// <summary></summary>
        public BookmarkCollection DynBookmarkCollection
        {
            get
            {
                BookmarkCollection bookmarkCollection = new BookmarkCollection();
                if (Loaded)
                {
                    string bookmarkLabel = "• HomeNetz";

                    Bookmark b1 = new Bookmark();
                    b1.ID = 0;
                    b1.DisplayName = string.Format("FTP: ftp://{0}@{1}", User, Host);
                    b1.Href = string.Format("ftp://{0}@{1}", User, Host);
                    b1.LabelsString = bookmarkLabel;
                    bookmarkCollection.Add(b1);

                    Bookmark b2 = new Bookmark();
                    b2.ID = 0;
                    b2.DisplayName = string.Format("FritzBox: https://{0}", Host);
                    b2.Href = string.Format("https://{0}", Host);
                    b2.LabelsString = bookmarkLabel;
                    bookmarkCollection.Add(b2);

                    Bookmark b3 = new Bookmark();
                    b3.ID = 0;
                    b3.DisplayName = "SamrtDrive: https://sd2.1und1.de (ka5300-051@online.de)";
                    b3.Href = "https://sd2.1und1.de";
                    b3.LabelsString = bookmarkLabel;
                    bookmarkCollection.Add(b3);

                }

                return bookmarkCollection;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataPath"></param>
        /// <param name="fileName"></param>
        public DynDNSController(string dataPath, string fileName)
        {
            _dataPath = dataPath;
            _fileName = fileName;

            LoadFile(_dataFullFileName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private bool LoadFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load(fileName);
                    User = doc.GetElementsByTagName("username")[0].InnerText;
                    Host = doc.GetElementsByTagName("homehost")[0].InnerText;
                    LastUpdate = doc.GetElementsByTagName("lastupdate")[0].InnerText;

                    Loaded = true;
                }
                catch
                {
                    Loaded = false;
                }
            }
            else
            {
                Loaded = false;
            }
            return Loaded;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="remoteAddr"></param>
        public void UpdateClientData(string username, string remoteAddr)
        {
            if (File.Exists(_dataFullFileName))
            {
                File.Delete(_dataFullFileName);
            }

            StringBuilder sbData = new StringBuilder();

            sbData.AppendLine("<root>");
            sbData.AppendLine("   <lastupdate>" + DAO.ClientDateTimeNow.ToString() + "</lastupdate>");
            sbData.AppendLine("   <username>" + username + "</username>");
            sbData.AppendLine("   <homehost>" + remoteAddr + "</homehost>");
            sbData.AppendLine("</root>");

            StreamWriter sw = new StreamWriter(_dataFullFileName, false);
            sw.Write(sbData.ToString());
            sw.Close();

            LoadFile(_dataFullFileName);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="queryString">Request.QueryString</param>
        /// <param name="serverVariables">Request.ServerVariables</param>
        public void UpdateRequestData(Dictionary<string, string> queryString, Dictionary<string, string> serverVariables)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("QUERYSTRING:");

            foreach (string key in queryString.Keys)
            {
                sb.AppendLine(string.Format("{0} = {1}", key, queryString[key]));
            }

            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("SERVERVARIABLES:");

            foreach (string key in serverVariables.Keys)
            {
                if (serverVariables[key].Trim().Length > 0)
                    sb.AppendLine(string.Format("{0} = {1}", key, serverVariables[key]));
            }

            sb.AppendLine("");

            StreamWriter sw = new StreamWriter(_logFullFileName, false);
            sw.Write(sb.ToString());
            sw.Close();
        }
    }
}
